package com.sgb.goods.view.adapter.publish.pop;

import android.content.Context;
import android.widget.Toast;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsPopGoodsTypeChooseBinding;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.view.adapter.publish.GoodsTypeLeftAdapter;
import com.sgb.goods.view.adapter.publish.GoodsTypeRightAdapter;
import com.sgb.goods.widget.pop.GoodsPopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MLog;

import java.util.List;

/**
 * 商品类型选择pop
 * @author cyj
 */
public class GoodsTypePopWindow extends GoodsPopWindow<String> {

    private boolean isReset;
    private boolean isClickOkView;
    public GoodsTypeLeftAdapter leftAdapter;
    public GoodsTypeRightAdapter rightAdapter;
    private GoodsPopGoodsTypeChooseBinding mDataBinding;

    public GoodsTypePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        // 至于为什么在 popLayout() 函数里初始化适配器是因为 GoodsTypePopWindow 的构造函数底层 super 函数内调用了 popLayout(),
        // 而数据绑定需要在xml布局前创建好适配器
        leftAdapter = new GoodsTypeLeftAdapter();
        rightAdapter = new GoodsTypeRightAdapter();
        return R.layout.goods_pop_goods_type_choose;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        holder.setBinding(BR.gpw, this);
        mDataBinding = (GoodsPopGoodsTypeChooseBinding) holder.getmBinding();

        // 确认。确认时需要记录历史节点
        mDataBinding.okView.setOnClickListener(v -> {

            // 右侧适配器未选择，则给予提示
            if (null == rightAdapter.currentObj && !isReset) {
                Toast.makeText(mContext, "必须选择三级分类", Toast.LENGTH_SHORT).show();
                return;
            }
            isClickOkView = true;

            // 记录历史节点
            leftAdapter.setRecordPoint();
            rightAdapter.setRecordPoint();

            // 结果回调
            if (onResultClick != null) {
                if(null != leftAdapter.oldObj && null != rightAdapter.oldObj) {
                    // 左侧对象 | 右侧一级对象 | 右侧二级对象
                    onResultClick.result(leftAdapter.oldObj, rightAdapter.oldObj,
                            rightAdapter.oldObj.getDatas().get(rightAdapter.oldPosition));
                } else {
                    // 重置+点击确认的情况下走这里，注意外部接收数据做好判断。太复杂的弹窗了，又是取消又是重置
                    onResultClick.result(null, null, null);
                }
            }

            dissMiss();
        });

        // 取消。取消时不记录历史节点
        mDataBinding.resetView.setOnClickListener(view -> {
            if(this.isReset) {
                resetDataSelect();
            } else {
                dissMiss();
            }
        });

        // 弹窗关闭监听，设置不记录历史节点
        setOnDismissListener(() -> {
            if(!isClickOkView) {
                leftAdapter.cancelRecordPoint();
                rightAdapter.cancelRecordPoint();
            }
            isClickOkView = false;
        });

        // 左联动右边的适配器。为右边适配器填充新的数据源
        leftAdapter.setItemClickListener((adapter2, view, position) -> {
            if(null != leftAdapter.getItem(position)) {
                MLog.e("当前选中的cid1及参数：", "name:" + leftAdapter.getItem(position).name + " | cid1: " + leftAdapter.getItem(position).id);
                rightAdapter.setNewData(leftAdapter.getItem(position).getDatas());
            }
        });
    }

    /** 重置数据选择为默认第一项 */
    private void resetDataSelect() {
        leftAdapter.reset(true);
        rightAdapter.reset(true);
        // 重新刷新数据
        setData(leftAdapter.getData(), this.isReset);
    }

    /** 设置左右适配器数据 */
    public void setData(List<MenuEntity1<MenuEntity1<MenuEntity1>>> dataList) {
        setData(dataList, false);
    }

    /**
     * 设置左右适配器数据
     * @param dataList    数据
     * @param isReset    是否含有重置功能
     */
    public void setData(List<MenuEntity1<MenuEntity1<MenuEntity1>>> dataList, boolean isReset) {

        this.isReset = isReset;
        if(isReset) {
            mDataBinding.resetView.setText("重置");
        }

        // 填充数据
        leftAdapter.setNewData(dataList);
        rightAdapter.setNewData(null == leftAdapter.oldObj ? dataList.get(0).getDatas() : leftAdapter.oldObj.getDatas());

        // 设置选中。左侧历史节点为空则默认第一个为选中项
        leftAdapter.setRecordPoint(null == leftAdapter.oldObj ? dataList.get(0) : leftAdapter.oldObj);
        rightAdapter.setRecordPoint(rightAdapter.oldObj);
    }
}
